package net.flintx.array;

/**
 * Created by flintx on 2018/1/13.
 */
public class MaximumAverageSubarrayI {
    public double findMaxAverage(int[] nums, int k) {
        if (nums.length == 1)
            return nums[0];
        for (int i = 1; i < nums.length; i++) {
            nums[i] = nums[i - 1] + nums[i];
        }
        int maxValue = Integer.MIN_VALUE;
        for (int i = k - 1; i < nums.length; i++) {
            int subSum = (i == k - 1)? nums[i] : nums[i] - nums[i - k];
            maxValue = Math.max(maxValue, subSum);
        }
        return Double.valueOf(maxValue) / Double.valueOf(k);
    }
}
